package gcontent.entities.content.gzip;

import java.util.zip.GZIPInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import gcontent.entities.Content;

public class ContentGZIP extends Content {
	
	public static final String TYPE_GZIP = "GZIP";
	
	public String getFormat()
	{
		return TYPE_GZIP;
	}

	public void parse()
	{
       GZIPInputStream in;
       ByteArrayOutputStream out;
       
       in = null;
       out = null;
       
       try {
    	   in = new GZIPInputStream(new ByteArrayInputStream(this.body));
    	   out = new ByteArrayOutputStream();
    
    	   // Transfer bytes from the compressed file to the output file
    	   byte[] buf = new byte[1024];
    	   int len;

    	   while ((len = in.read(buf)) > 0) {
    		   out.write(buf, 0, len);
    	   }
    	   
    	   body = out.toByteArray();
    	   //System.out.println(new String(body));System.exit(1);
    	   
       }catch(Exception ex) {
    	   ex.printStackTrace();
       } finally {
    	   
	       if(out != null) {
	    	   try { out.close(); } catch(IOException io_ex) {}
	       }
	       
    	   if(in != null)
    		   try { in.close(); } catch(IOException io_ex) {}    		  	    			  
       }
	}

}
